import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	console.log("结果：", list)
	let currentNodeid = bussiness.currentNodeid
	let uid = bussiness.docUnid
	// todo: 加载子表单项: 产品变更清单
	let type = formData.Type
	// todo: 指定子表单适用终端
	if(currentNodeid=="T10002"){
		// todo: 子表单项增加按钮
	}
	// todo: 暂存时加载客户信息数据
	let arr = list.map(item => {
		if(formData.cCusName){
			//回退回来的表单客户信息不能修改
			if(['cCusCode','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
			'Cust_TYPE','cCusDefine2','OldDepart'].indexOf(item.name) != -1){
				item.disabled = true
			}
		}
		return item
	})
	return arr
}

async function formonsubmit(list,formData,bussiness){
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//验证客户编码是否存在
		var cCusCode = formData.cCusCode;
		if(comCode=="0"){
			uni.showToast({
				title: "【请填写客户编码！】",
				icon:'none'
			})
			return false;
		}
		
		let searchField = {
			paramasMap: {},
			headersMap: {
				sysid: "bpm",
				syspwd: "pass",
				userid: user.bpmUid
			},
			url: bpmUrl + '/rest/appform/getCustomerInfo'
		}
		// 验证客户名称和编码是否对应
		await ajax({
			url:'/bpm/post',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					cCusName: formData.cCusName,
					cCusCode: formData.cCusCode
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B078'
			} },
			success:res => {
				let a = res.data.data
				if (a==0) {
					uni.showToast({
						title: "【客户名称和编码有误，请检查后重新输入！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		// 验证子表信息不为空
		await ajax({
			url:'/bpm/post',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					docUnid: bussiness.docUnid
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B177'
			} },
			success:res => {
				let a = res.data.data
				if (a==0) {
					uni.showToast({
						title: "【请填写表体信息！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		// 验证数量字段不能为空
		await ajax({
			url:'/bpm/post',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					docUnid: bussiness.docUnid
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B178'
			} },
			success:res => {
				let a = res.data.data
				if (a==1) {
					uni.showToast({
						title: "【请填写申请结算价！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		
	}
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
		'Cust_TYPE','cCusDefine2','OldDepart'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}

//模糊搜索客户列表
function getCustomerName(queryString,cb) {}
function getCustomerNameQuery(queryString, formData,cb) {
	console.log("hello", formData,cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString,
			DepartmentID: formData.DepartmentID
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B063'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			let data = []
			if (res.data.data && res.data.data.length > 0) {
				res.data.data.map(item => {
					let temp = {}
					temp["value"] = item.cCusCode
					temp["name"] = item.cCusName
					temp["extraInfo"] = {
						comCode1: item.comCode1
					}
					data.push(temp)
				})
			} 
			cb(data)
		}
	})
		// ajax().then((res)=>{
		// 	// 对返回数据格式化成(必须包含value值): [{value: "",...},...]
		// 	cb(data)
		// })
}

//给相关字段赋值
async function fz($event, item,formData,list){
	let user = getUserInfo()
	// 客户名称相关赋值
	formData.cCusCode = event.value
	formData.comCode1 = event.extraInfo.comCode1
	let searchField = {
		paramasMap: {},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + ''
	}
	// 省份(Department)
	formData.Department = '北京'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B049'
		} },
		success:res => {
			formData.Department = res.data.data
		}
	})
	// 客户分类(cCusDefine6)
	formData.cCusDefine6 = '类型1'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B050'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 传真号码(cCusFax)
	formData.cCusFax = '123'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B051'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 电话(cCusPhone)
	formData.cCusPhone = '010'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B052'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 手机(cCusHand)
	formData.cCusHand = '131'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B053'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 客户类型(Cust_TYPE)
	formData.Cust_TYPE = '客户类型'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B163'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 开票类型(cCusDefine2)
	formData.cCusDefine2 = '票据1'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B056'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 开票方式(Invoice_Way)
	formData.cCusDefine2 = '票据1'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B057'
		} },
		success:res => {
			formData.Invoice_Way = res.data.data
		}
	})
	// 根据被授权医院名称返回其所对应的大区
	formData.ccdefine7 = '票据1'
	await ajax({
		url:'/bpm/post',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				a: formData.Department,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
		} },
		success:res => {
			formData.OldDepart = res.data.data
		}
	})
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
		'Cust_TYPE','cCusDefine2','Invoice_Way','OldDepart'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
}

export default {
	formonload,
	formonsubmit,
	getCustomerName,
	getCustomerNameQuery,
	fz
}